#!/bin/bash

chmod +x scripts/common_scripts/common.sh
source scripts/common_scripts/common.sh

set -e
set -o pipefail

handle_error() {
  local exit_code=$?
  echo "Error occurred in script at line: ${1} with exit code: ${exit_code}"
  exit $exit_code
}

trap 'handle_error ${LINENO}' ERR

function work() {
  cur_path=$(realpath .)
  par_path=$(dirname $cur_path)

  echo "1. requirements"
  pip3 install -r scripts/publish_pipeline/publish_as_apt_source/requirements.txt
  cp scripts/utils/patch/client.py /usr/local/lib/python3.12/dist-packages/obs/client.py
  chmod +r /usr/local/lib/python3.12/dist-packages/obs/client.py

  echo "2. start upload"
  cd $cur_path
  python3 scripts/publish_pipeline/publish_as_apt_source/publishers/studio-upload.py --codename="${codename}" --component="${component}" --arch="${arch}"
}

function main() {
  echo "=== PUBLISH STUDIO START ==="

  prepare_software
  prepare_git $gitcode_username $gitcode_token
  work

  echo "=== PUBLISH STUDIO END ==="
}

main

exit 0